package com.fuint.module.backendApi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.unmarshaller.JsonUnmashaller;
import com.fuint.common.dto.SettingNameValueDTO;
import com.fuint.common.enums.SettingTypeEnum;
import com.fuint.common.service.SettingService;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.fuint.repository.model.MtSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 商户结算管理
 *
 * @author Tom
 * @since 2025/02/17
 */
@Api(tags = "管理端-全局配置相关接口")
@RestController
@RequestMapping(value = "/backendApi/setting")
@Slf4j
public class BackendSettingController extends BaseController {

    private final SettingService settingService;

    @Autowired
    BackendSettingController(SettingService settingService) {
        this.settingService = settingService;
    }

    @ApiOperation(value = "配置详情")
    @GetMapping
    @CrossOrigin
    public ResponseObject detail() throws BusinessCheckException {
        // common 全局配置（只暴露给服务端的）
        List<MtSetting> common = settingService.getSettingList(0, SettingTypeEnum.COMMON.getKey());
        if (CollUtil.isEmpty(common)) {
            return getSuccessResult("");
        }
        // front_global 前端全局配置（只暴露给前端的）
        common.addAll(settingService.getSettingList(0, SettingTypeEnum.FRONT_GLOBAL.getKey()));

        Map<String, String> result = new HashMap<>();
        for (MtSetting setting : common) {
            if (Objects.isNull(setting)) {
                continue;
            }

            result.put(setting.getName(), setting.getValue());
        }
        return getSuccessResult(result);
    }

    @ApiOperation(value = "保存配置")
    @PostMapping
    @CrossOrigin
    public ResponseObject save(@RequestBody Map<String, String> dto) throws BusinessCheckException {
        if (Objects.isNull(dto)) {
            return getSuccessResult("");
        }

        for (String key : dto.keySet()) {
            if (StrUtil.isEmpty(key)) {
                continue;
            }
            MtSetting commonSetting = settingService.querySettingByName(0, SettingTypeEnum.COMMON.getKey(), key);
            if (Objects.nonNull(commonSetting)) {
                commonSetting.setValue(dto.get(key));
                try {
                    settingService.saveSetting(commonSetting);
                } catch (Exception e) {
                    log.error(JSONUtil.toJsonStr(e));
                }
            }
            MtSetting frontGlobalSetting = settingService.querySettingByName(0, SettingTypeEnum.FRONT_GLOBAL.getKey(), key);
            if (Objects.nonNull(frontGlobalSetting)) {
                frontGlobalSetting.setValue(dto.get(key));
                try {
                    settingService.saveSetting(frontGlobalSetting);
                } catch (Exception e) {
                    log.error(JSONUtil.toJsonStr(e));
                }
            }
        }

        return getSuccessResult("");
    }

}
